﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Registered_Student_Management
{
    public partial class F_Main : Form
    {
        string connection_string =  "Provider=Microsoft.Jet.OleDb.4.0;Data Source=Database.mdb;Jet OLEDB:Database Password=51002201";

        int lvl; 
        int center = 1;
        int ChangeRatio;
        
        bool gettotextbox = false;

        public F_Main()
        {
            InitializeComponent();
        }
//---------------------------------------------------------------------------Main---------------------------------------------------------------------------
        private void F_Main_Load(object sender, EventArgs e)
        {
            try
            {
                GB_Login.Visible = true;
                GB_Main.Visible = false;
                GB_TrangChu.Visible = false;
                PN_register.Visible = false;
                GB_HR.Visible = false;
                GB_Class.Visible = false;
                GB_Student.Visible = false;
                GB_Course.Visible = false;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

//---------------------------------------------------------------------------Login--------------------------------------------------------------------------------
        private void BT_Login_Click(object sender, EventArgs e)
        {
            
            try
            {
                OleDbConnection cnlogin = new OleDbConnection();
                cnlogin.ConnectionString = connection_string;
                cnlogin.Open();
                OleDbCommand commandlogin = new OleDbCommand();
                commandlogin.Connection = cnlogin;
                commandlogin.CommandText = "select * from [user] where username = @username and password = @password";
                commandlogin.Parameters.AddWithValue("@username", TB_Name.Text);
                commandlogin.Parameters.AddWithValue("@password", TB_Pass.Text);
                OleDbDataAdapter adapt = new OleDbDataAdapter(commandlogin);
                DataTable data = new DataTable();
                adapt.Fill(data);
/*              OleDbDataReader reader = commandlogin.ExecuteReader();
//              while (reader.Read())
//              {*/
                if (data.Rows.Count >0)
                {
                    DataRow dr = data.Rows[0];
                    lvl = int.Parse(dr["level"].ToString());
                    if (lvl == 3 || lvl == 4)
                    {
                        GB_Login.Visible = false;
                        GB_Main.Visible = true;
                        PN_register.Visible = true;
                        GB_TrangChu.Visible = false;
                        GB_HR.Visible = false;
                        GB_Class.Visible = false;
                        GB_Student.Visible = false;
                        MessageBox.Show("Đăng Nhập Thành Công");
                    }
                    else
                    {
                        GB_Login.Visible = false;
                        GB_Main.Visible = true;
                        PN_register.Visible = false;
                        GB_TrangChu.Visible = true;
                        GB_HR.Visible = false;
                        GB_Class.Visible = false;
                        GB_Student.Visible = false;
                        MessageBox.Show("Đăng Nhập Thành Công");
                    }
                    
                }
                else
                {
                    if (MessageBox.Show("Đăng Nhập Thất Bại, Bạn có muốn Đăng Nhập lại không?", "Đăng Nhập", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        TB_Name.Focus();
                    }
                    else
                    {
                        Close();
                        System.Windows.Forms.Application.Exit();
                    }
                }
//              }
                cnlogin.Close();
                cnlogin.Dispose();
                cnlogin = null;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
           
        }

        private void PN_register_VisibleChanged(object sender, EventArgs e)
        {
            DTP_ngaysinhdangky.Value = DateTime.Now;
            string[] s = { "Nam", "Nữ" };

            this.CbB_gioitinhdangky.DataSource = s;

        }
//----------------------------------------------------------------------------Register--------------------------------------------------------------------------------
        private void BT_register_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnInsert = new OleDbConnection();
                cnInsert.ConnectionString = connection_string;
                cnInsert.Open();
                OleDbCommand commandInsertUser = new OleDbCommand();
                commandInsertUser.Connection = cnInsert;

                string birthday1 = DTP_ngaysinhdangky.Value.ToShortDateString();
                string username1 = TB_tendangky.Text, password1 = TB_matkhaudangky.Text, fullname1 = TB_fullnamedangky.Text, gender1 = CbB_gioitinhdangky.Text, phone1 = TB_sdtdangky.Text, address1 = TB_diachidangky.Text;
                int level1 = int.Parse(TB_leveldangky.Text.ToString());

                //('" + username + "','" + password + "','" + fullname + "','" + birthday + "','" + gender + "','" + address + "','" + phone + "','" + level + "')";
                //( @username, @password, @fullname, @birthday, @gender,@address, @phone, @level)
                commandInsertUser.CommandText = "insert into [user] ( [username], [password], [fullname], [birthday], [gender], [address], [phone], [level]) values('" + username1 + "','" + password1 + "','" + fullname1 + "','" + birthday1 + "','" + gender1 + "','" + address1 + "','" + phone1 + "','" + level1 + "')";

                commandInsertUser.ExecuteNonQuery();

                cnInsert.Close();
                cnInsert.Dispose();
                cnInsert = null;

                MessageBox.Show("Đăng Ký Thành Công", "Thông Báo");
                Close();
                TB_tendangky.Clear(); TB_matkhaudangky.Clear(); TB_fullnamedangky.Clear(); TB_sdtdangky.Clear(); TB_diachidangky.Clear(); TB_leveldangky.Clear();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
//---------------------------------------------------------------------------MenuMain-------------------------------------------------------------------------
        private void HomeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvl == 3 || lvl == 4)
                {
                    GB_Login.Visible = false;
                    GB_Main.Visible = true;
                    PN_register.Visible = true;
                    GB_TrangChu.Visible = false;
                    GB_HR.Visible = false;
                    GB_Class.Visible = false;
                    GB_Student.Visible = false;
                }
                else
                {
                    GB_Login.Visible = false;
                    GB_Main.Visible = true;
                    PN_register.Visible = false;
                    GB_TrangChu.Visible = true;
                    GB_HR.Visible = false;
                    GB_Class.Visible = false;
                    GB_Student.Visible = false;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void StudentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GB_Login.Visible = false;
                GB_Main.Visible = true;
                PN_register.Visible = false;
                GB_TrangChu.Visible = false;
                GB_HR.Visible = false;
                GB_Class.Visible = false;
                GB_Student.Visible = true;
                GB_Course.Visible = false;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void HRToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GB_Login.Visible = false;
                GB_Main.Visible = true;
                PN_register.Visible = false;
                GB_HR.Visible = true;
                GB_TrangChu.Visible = false;
                GB_Class.Visible = false;
                GB_Student.Visible = false;
                GB_Course.Visible = false;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void ClassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GB_Login.Visible = false;
                PN_register.Visible = false;
                GB_Main.Visible = true;
                GB_TrangChu.Visible = false;
                GB_HR.Visible = false;
                GB_Class.Visible = true;
                GB_Student.Visible = false;
                GB_Course.Visible = false;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void CourseToolStripMenuItemm_Click(object sender, EventArgs e)
        {
            try
            {
                GB_Login.Visible = false;
                GB_Main.Visible = true;
                PN_register.Visible = false;
                GB_TrangChu.Visible = false;
                GB_HR.Visible = false;
                GB_Class.Visible = false;
                GB_Student.Visible = false;
                GB_Course.Visible = true;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

//--------------------------------------------------------GB_Student-----------------------------------------------------//
        //-----------------------------------------------------------------------------------------------------//

        private void GB_Student_VisibleChanged(object sender, EventArgs e)
        {
            RBT_tatcaHV_CheckedChanged(sender, e);
            DT_ngaysinhHV.Value = DateTime.Now;
        }

        private void DGV_danhsachHV_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (gettotextbox == true)
                {
                    try
                    {
                        // load data to combobox
                        string[] s = { "Nam", "Nữ" };

                        this.CB_gioitinhHV.DataSource = s;
                        
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show(Ex.Message);
                    }
                    int i = DGV_danhsachHV.CurrentCell.RowIndex;
                    TB_maHV.Text = DGV_danhsachHV.Rows[i].Cells[0].Value.ToString();
                    TB_tenHV.Text = DGV_danhsachHV.Rows[i].Cells[1].Value.ToString();
                    CB_gioitinhHV.Text = DGV_danhsachHV.Rows[i].Cells[2].Value.ToString();
                    DT_ngaysinhHV.Text = DGV_danhsachHV.Rows[i].Cells[3].Value.ToString();
                    TB_sdtHV.Text = DGV_danhsachHV.Rows[i].Cells[5].Value.ToString();
                    TB_nghenghiepHV.Text = DGV_danhsachHV.Rows[i].Cells[4].Value.ToString();
                    TB_diachiHV.Text = DGV_danhsachHV.Rows[i].Cells[6].Value.ToString();
                    TB_emailHV.Text = DGV_danhsachHV.Rows[i].Cells[7].Value.ToString();

                }
                else
                {
                    try
                    {
                        // load data to combobox
                        string[] s = { "waiting", "learning", "pause", "finish" };

                        this.CB_trangthaiHV.DataSource = s;
                        
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show(Ex.Message);
                    }
                    int j = DGV_danhsachHV.CurrentCell.RowIndex;
                    TB_matheHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[0].Value.ToString();
                    TB_maHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[1].Value.ToString();
                    TB_tenHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[2].Value.ToString();
                    TB_malopHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[3].Value.ToString();
                    TB_tenlopHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[4].Value.ToString();
                    CB_trangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[5].Value.ToString();
                    DT_ngaykhoitaoHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[6].Value.ToString();
                    TB_nguoikhoitaoHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[7].Value.ToString();

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void RBT_tatcaHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hv(sender,e);
            PN_themHV.Visible = true;
            PN_themHV.Enabled = true;
            PN_trangthaiHV.Visible = false;
            PN_trangthaiHV.Enabled = false;
            BT_doitrangthaiHV.Visible = false;
            BT_doithongtinHV.Visible = true;
            BT_timkiemHV.Enabled = true;
            gettotextbox = true;
            TB_tenHV.Clear(); TB_sdtHV.Clear(); TB_nghenghiepHV.Clear(); TB_diachiHV.Clear(); TB_maHV.Clear(); TB_emailHV.Clear();
        }

        private void RBT_danghocHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hvdanghoc(sender, e);
            PN_themHV.Enabled = false;
            PN_themHV.Visible = false;
            PN_trangthaiHV.Visible = true;
            PN_trangthaiHV.Enabled = true;
            BT_doitrangthaiHV.Visible = true;
            BT_doithongtinHV.Visible = false;
            BT_timkiemHV.Enabled = false;
            gettotextbox = false;
            ChangeRatio = 1;
            TB_tenHVtrangthaiHV.Clear();  TB_maHVtrangthaiHV.Clear(); TB_malopHVtrangthaiHV.Clear(); TB_tenlopHVtrangthaiHV.Clear(); TB_nguoikhoitaoHVtrangthaiHV.Clear();
        }
        private void RBT_waitingHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hvdangcho(sender, e);
            PN_themHV.Enabled = false;
            PN_themHV.Visible = false;
            PN_trangthaiHV.Visible = true;
            PN_trangthaiHV.Enabled = true;
            BT_doitrangthaiHV.Visible = true;
            BT_doithongtinHV.Visible = false;
            BT_timkiemHV.Enabled = false;
            gettotextbox = false;
            ChangeRatio = 2;
            TB_tenHVtrangthaiHV.Clear(); TB_maHVtrangthaiHV.Clear(); TB_malopHVtrangthaiHV.Clear(); TB_tenlopHVtrangthaiHV.Clear(); TB_nguoikhoitaoHVtrangthaiHV.Clear();
        }

        private void RBT_pauseHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hvtamdung(sender, e);
            PN_themHV.Enabled = false;
            PN_themHV.Visible = false;
            PN_trangthaiHV.Visible = true;
            PN_trangthaiHV.Enabled = true;
            BT_doitrangthaiHV.Visible = true;
            BT_doithongtinHV.Visible = false;
            BT_timkiemHV.Enabled = false;
            gettotextbox = false;
            ChangeRatio = 4;
            TB_tenHVtrangthaiHV.Clear(); TB_maHVtrangthaiHV.Clear(); TB_malopHVtrangthaiHV.Clear(); TB_tenlopHVtrangthaiHV.Clear(); TB_nguoikhoitaoHVtrangthaiHV.Clear();
        }

        private void RBT_finishHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hvhoanthanh(sender, e);
            PN_themHV.Enabled = false;
            PN_themHV.Visible = false;
            PN_trangthaiHV.Visible = true;
            PN_trangthaiHV.Enabled = true;
            BT_doitrangthaiHV.Visible = true;
            BT_doithongtinHV.Visible = false;
            BT_timkiemHV.Enabled = false;
            gettotextbox = false;
            ChangeRatio = 3;
            TB_tenHVtrangthaiHV.Clear(); TB_maHVtrangthaiHV.Clear(); TB_malopHVtrangthaiHV.Clear(); TB_tenlopHVtrangthaiHV.Clear(); TB_nguoikhoitaoHVtrangthaiHV.Clear();
        }    
        
        private void load_datagrid_hv(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from student";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        
        private void load_datagrid_hvdanghoc(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from job where status = 'learning' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        
        private void load_datagrid_hvdangcho(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from job where status = 'waiting' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_hvtamdung(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from job where status = 'pause' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_hvhoanthanh(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from job where status = 'finish' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_timkiemhv(object sender, EventArgs e)
        {
            try
            {
                if (TB_timkiemHV.Text == "")
                {
                    MessageBox.Show("Nhập Tên Học Viên", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cn = new OleDbConnection();
                    cn.ConnectionString = connection_string;
                    OleDbCommand commandtimkiemHV = new OleDbCommand();
                    commandtimkiemHV.Connection = cn;
                    string fullname = TB_timkiemHV.Text;
                    commandtimkiemHV.CommandText = "select * from student where fullname = '" + fullname + "'";
                    OleDbDataAdapter adapt = new OleDbDataAdapter(commandtimkiemHV);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    DGV_danhsachHV.DataSource = dt;
                    cn.Close();
                    cn.Dispose();
                    cn = null;
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_doithongtinHV_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_tenHV.Text == "" || CB_gioitinhHV.Text == "" || DT_ngaysinhHV.Text == "" || TB_sdtHV.Text == "" || TB_nghenghiepHV.Text == "" || TB_diachiHV.Text == "")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cnUpdate = new OleDbConnection();
                    cnUpdate.ConnectionString = connection_string;
                    cnUpdate.Open();
                    OleDbCommand cmdUpdateHV = new OleDbCommand();
                    cmdUpdateHV.Connection = cnUpdate;
                    int id = int.Parse(TB_maHV.Text.ToString());
                    string fullname = TB_tenHV.Text, gender = CB_gioitinhHV.Text, career = TB_nghenghiepHV.Text, phone = TB_sdtHV.Text, address = TB_diachiHV.Text,email = TB_emailHV.Text;
                    string birthday = DT_ngaysinhHV.Value.ToShortDateString();
                    cmdUpdateHV.CommandText = "UPDATE student SET fullname ='" + fullname + "',gender ='" + gender + "',career ='" + career + "',birthday ='" + birthday + "',phone ='" + phone + "',address ='" + address + "',email = '" + email +"' WHERE id = " + id;
                    cmdUpdateHV.ExecuteNonQuery();
                    cnUpdate.Close();
                    load_datagrid_hv(sender, e);
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_doitrangthaiHV_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_tenHVtrangthaiHV.Text == "" || TB_maHVtrangthaiHV.Text == "" || TB_malopHVtrangthaiHV.Text == "" || TB_tenlopHVtrangthaiHV.Text == "" || TB_nguoikhoitaoHVtrangthaiHV.Text == "" || DT_ngaykhoitaoHVtrangthaiHV.Text == "")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                
                else
                {
                    OleDbConnection cnUpdateTT = new OleDbConnection();
                    cnUpdateTT.ConnectionString = connection_string;
                    cnUpdateTT.Open();
                    OleDbCommand cmdUpdateTTHV = new OleDbCommand();
                    cmdUpdateTTHV.Connection = cnUpdateTT;
                    int id = int.Parse(TB_matheHVtrangthaiHV.Text.ToString());
                    int maHV = int.Parse(TB_maHVtrangthaiHV.Text.ToString());
                    int maLop = int.Parse(TB_malopHVtrangthaiHV.Text.ToString());
                    string tenHV = TB_tenHVtrangthaiHV.Text, tenLop = TB_tenlopHVtrangthaiHV.Text, creater = TB_nguoikhoitaoHVtrangthaiHV.Text;
                    string create_day = DT_ngaykhoitaoHVtrangthaiHV.Value.ToShortDateString();
                    string status = CB_trangthaiHV.Text;
                    cmdUpdateTTHV.CommandText = "UPDATE job SET id_student ='" + maHV + "', name_student ='" + tenHV + "',id_class ='" + maLop + "', name_class ='" + tenLop + "',status = '" + status + "',create_date ='" + create_day + "' ,create_person ='" + creater + "'WHERE id = " + id;
                    cmdUpdateTTHV.ExecuteNonQuery();
                    cnUpdateTT.Close();
                    if (ChangeRatio == 1)
                    {
                        load_datagrid_hvdanghoc(sender, e);
                    }
                    else if (ChangeRatio == 2)
                    {
                        load_datagrid_hvdangcho(sender, e);
                    }
                    else if (ChangeRatio == 3)
                    {
                        load_datagrid_hvhoanthanh(sender, e);
                    }
                    else if (ChangeRatio == 4)
                    {
                        load_datagrid_hvtamdung(sender, e);
                    }
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        
        private void BT_timkiemHV_Click(object sender, EventArgs e)
        {
            gettotextbox = true;
            load_datagrid_timkiemhv(sender, e);
        }


//--------------------------------------------------------GB_class-----------------------------------------------------//
        //-----------------------------------------------------------------------------------------------------//

        private void GB_Class_VisibleChanged(object sender, EventArgs e)
        {
            RB_tatcacacLop_CheckedChanged(sender, e);
        }

        private void DGV_danhsachLop_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                OleDbConnection cnData = new OleDbConnection();
                cnData.ConnectionString = connection_string;
                OleDbCommand commandData1 = new OleDbCommand();
                commandData1.Connection = cnData;
                commandData1.CommandText = "select id, fullname from teacher";
                OleDbDataAdapter adapt1 = new OleDbDataAdapter(commandData1);
                DataSet ds1 = new DataSet();
                adapt1.Fill(ds1);

                CB_tengiaovien.DataSource = ds1.Tables[0];
                CB_tengiaovien.DisplayMember = "fullname";
                CB_tengiaovien.ValueMember = "id";

                // load data to combobox sobuoihoc
                string[] s = { "S246", "C246", "T246", "S357", "C357", "T357" };

                this.CB_sobuoihoc.DataSource = s;
               
                int i = DGV_danhsachLop.CurrentCell.RowIndex;
                TB_maLop.Text = DGV_danhsachLop.Rows[i].Cells[0].Value.ToString();
                TB_kyhieuLop.Text = DGV_danhsachLop.Rows[i].Cells[1].Value.ToString();
                TB_tenLop.Text = DGV_danhsachLop.Rows[i].Cells[2].Value.ToString();
                TB_soluonghvLop.Text = DGV_danhsachLop.Rows[i].Cells[3].Value.ToString();
                TB_gioihanhvLop.Text = DGV_danhsachLop.Rows[i].Cells[4].Value.ToString();
                CB_tengiaovien.Text = DGV_danhsachLop.Rows[i].Cells[6].Value.ToString();
                CB_sobuoihoc.Text = DGV_danhsachLop.Rows[i].Cells[7].Value.ToString();
                TB_khoahocLop.Text = DGV_danhsachLop.Rows[i].Cells[8].Value.ToString();
                int state = int.Parse(DGV_danhsachLop.Rows[i].Cells[9].Value.ToString());
                if (state == 0)
                {
                    ChB_tinhtrangLop.Checked = false;
                    if(lvl== 3 || lvl == 4) BT_moLopfull.Enabled = true;
                    else BT_moLopfull.Enabled = false;
                }
                else if (state == 1)
                {
                    ChB_tinhtrangLop.Checked = true;
                    BT_moLopfull.Enabled = true;
                }
                                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
            
        }

        
        private void RB_tatcacacLop_CheckedChanged(object sender, EventArgs e)
        {
            
            load_datagrid_tatcalop(sender, e);
            BT_xoaLop.Enabled = false;
            BT_moLopfull.Enabled = false;
            BT_themHV.Enabled = false;
            TB_gioihanhvLop.Enabled = false;
            TB_kyhieuLop.Enabled = false;
            TB_soluonghvLop.Enabled = false;
            CB_tengiaovien.Enabled = true;
            CB_sobuoihoc.Enabled = true;
            TB_soluonghvLop.Clear(); TB_gioihanhvLop.Clear(); TB_kyhieuLop.Clear(); TB_maLop.Clear(); TB_tenLop.Clear();
        }

        private void RB_dangmoLop_CheckedChanged(object sender, EventArgs e)
        {
            
            load_datagrid_lopdanghoc(sender, e);
            BT_xoaLop.Enabled = false;
            BT_moLopfull.Enabled = false;
            BT_themHV.Enabled = false;
            TB_gioihanhvLop.Enabled = false;
            TB_kyhieuLop.Enabled = false;
            TB_soluonghvLop.Enabled = false;
            CB_tengiaovien.Enabled = true;
            CB_sobuoihoc.Enabled = true;
            TB_soluonghvLop.Clear(); TB_gioihanhvLop.Clear(); TB_kyhieuLop.Clear(); TB_maLop.Clear(); TB_tenLop.Clear();
        }

        private void RB_dukienLop_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                
                load_datagrid_lopdangcho(sender, e);
                BT_xoaLop.Enabled = true;
                BT_moLopfull.Enabled = false;
                BT_themHV.Enabled = true;
                TB_gioihanhvLop.Enabled = true;
                TB_kyhieuLop.Enabled = true;
                TB_soluonghvLop.Enabled = false;
                CB_tengiaovien.Enabled = true;
                CB_sobuoihoc.Enabled = true;
                TB_soluonghvLop.Clear(); TB_gioihanhvLop.Clear(); TB_kyhieuLop.Clear(); TB_maLop.Clear(); TB_tenLop.Clear();
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        
        private void load_datagrid_tatcalop(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from class";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachLop.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_lopdanghoc(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from class where available = 1 ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachLop.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_lopdangcho(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from class where available = 0 ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachLop.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_timkiemLop(object sender, EventArgs e)
        {
            try
            {
                if (TB_timkiemLop.Text == "")
                {
                    MessageBox.Show("Nhập Tên Lớp", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cn = new OleDbConnection();
                    cn.ConnectionString = connection_string;
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = cn;
                    string ccode = TB_timkiemLop.Text;
                    command.CommandText = "select * from class where ccode = '" + ccode +"'";
                    OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    DGV_danhsachLop.DataSource = dt;
                    cn.Close();
                    cn.Dispose();
                    cn = null;
                   
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_danhsachHV_Click(object sender, EventArgs e)
        {
            try
            {
                F_ListStudentOfClass obj = new F_ListStudentOfClass();
                obj.truyendata(TB_maLop.Text);
                obj.ShowDialog(this);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        

        private void BT_themHV_Click(object sender, EventArgs e)
        {
            try
            {
                if (ChB_tinhtrangLop.Checked == true)
                {
                    MessageBox.Show("Lớp Đã Đủ Học Viên", "Thông Báo");
                }
                else
                {
                    
                    F_NewStudent obj = new F_NewStudent();
                    obj.getIDclass(TB_maLop.Text);
                    obj.getNAMEclass(TB_kyhieuLop.Text);
                    obj.getIDcourse(TB_khoahocLop.Text);
                    obj.ShowDialog(this);
                    load_datagrid_lopdangcho(sender, e);
                }
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


        private void BT_xoaLop_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnDel = new OleDbConnection();
                cnDel.ConnectionString = connection_string;
                cnDel.Open();
                OleDbCommand cmdDelHV = new OleDbCommand();
                cmdDelHV.Connection = cnDel;
                int id = int.Parse(TB_maLop.Text.ToString());
                cmdDelHV.CommandText = "DELETE * FROM class WHERE id = " + id;
                cmdDelHV.ExecuteNonQuery();
                cnDel.Close();
                load_datagrid_lopdangcho(sender, e);

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_moLopfull_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnUpdateLop = new OleDbConnection();
                cnUpdateLop.ConnectionString = connection_string;
                cnUpdateLop.Open();
                OleDbCommand cmdUpdateTTL = new OleDbCommand();
                cmdUpdateTTL.Connection = cnUpdateLop;
                int id = int.Parse(TB_maLop.Text.ToString());
                cmdUpdateTTL.CommandText = "UPDATE class SET available = 1 WHERE id = " + id;
                cmdUpdateTTL.ExecuteNonQuery();

                OleDbCommand cmdUpdateCourse = new OleDbCommand();
                cmdUpdateCourse.Connection = cnUpdateLop;
                int idCourse = int.Parse(TB_khoahocLop.Text.ToString());
                cmdUpdateCourse.CommandText = "UPDATE course SET available = 1 WHERE id = " + idCourse;
                cmdUpdateCourse.ExecuteNonQuery();

                cnUpdateLop.Close();
                load_datagrid_lopdanghoc(sender, e);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        
        private void BT_khoamoitrongLop_Click(object sender, EventArgs e)
        {
            GB_Login.Visible = false;
            GB_Main.Visible = true;
            GB_TrangChu.Visible = false;
            GB_HR.Visible = false;
            GB_Class.Visible = false;
            GB_Student.Visible = false;
            GB_Course.Visible = true;

        }

        private void BT_doithongtinLop_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_soluonghvLop.Text == "" || TB_gioihanhvLop.Text == "" || TB_kyhieuLop.Text=="")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cnUpdateLop = new OleDbConnection();
                    cnUpdateLop.ConnectionString = connection_string;
                    cnUpdateLop.Open();
                    OleDbCommand cmdUpdateTTLop = new OleDbCommand();
                    cmdUpdateTTLop.Connection = cnUpdateLop;

                    int id = int.Parse(TB_maLop.Text.ToString());
                    int limit_student1 = int.Parse(TB_gioihanhvLop.Text.ToString());
                    int student_number1 = int.Parse(TB_soluonghvLop.Text.ToString());
                    int id_teacher1 = int.Parse(CB_tengiaovien.SelectedValue.ToString());

                    int belong1 = int.Parse(TB_khoahocLop.Text.ToString());
                    string ccode1 = TB_kyhieuLop.Text, time1 = CB_sobuoihoc.Text, name_teacher1 = CB_tengiaovien.Text;
                    string cname1 = TB_tenLop.Text;

                    cmdUpdateTTLop.CommandText = " update [class] set [ccode] = '" + ccode1 + "' ,[cname] = '" + cname1 + "' ,[student_number] = '" + student_number1 + "' , [limit_student] =  '" + limit_student1 + "' , [id_teacher] = '" + id_teacher1 + "', [name_teacher] = '" + name_teacher1 + "' , [time] = '" + time1 + "',[belong] = '"+ belong1+"' where id = " + id;

                    cmdUpdateTTLop.ExecuteNonQuery();
                    cnUpdateLop.Close();
                    load_datagrid_tatcalop(sender, e);
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


        private void BT_timkiemLop_Click(object sender, EventArgs e)
        {
            load_datagrid_timkiemLop(sender, e);
        }

//--------------------------------------------------------GB_HR-----------------------------------------------------//
        //-----------------------------------------------------------------------------------------------------//


        private void GB_HR_VisibleChanged(object sender, EventArgs e)
        {
            RBT_tatcaHR_CheckedChanged(sender, e);
        }

        private void DGV_danhsachHR_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                string[] s = { "nam", "nữ" };

                this.CbB_gioitinhHR.DataSource = s;

                string[] x = { "giảng viên", "nhân viên" };

                this.CbB_chucvuHR.DataSource = x;

                int k = DGV_danhsachHR.CurrentCell.RowIndex;
                TB_maHR.Text = DGV_danhsachHR.Rows[k].Cells[0].Value.ToString();
                TB_hovatenHR.Text = DGV_danhsachHR.Rows[k].Cells[1].Value.ToString();
                CbB_gioitinhHR.Text = DGV_danhsachHR.Rows[k].Cells[2].Value.ToString();
                TB_sdtHR.Text = DGV_danhsachHR.Rows[k].Cells[3].Value.ToString();
                TB_emailHR.Text = DGV_danhsachHR.Rows[k].Cells[4].Value.ToString();
                TB_diachiHR.Text = DGV_danhsachHR.Rows[k].Cells[5].Value.ToString();
                DTP_ngaysinhHR.Text = DGV_danhsachHR.Rows[k].Cells[6].Value.ToString();
                CbB_chucvuHR.Text = DGV_danhsachHR.Rows[k].Cells[7].Value.ToString();
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }

        }

        private void RBT_tatcaHR_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_tatcaHR(sender, e);
            TB_maHR.Clear(); TB_hovatenHR.Clear(); TB_sdtHR.Clear(); TB_emailHR.Clear(); TB_diachiHR.Clear(); 
        }

        private void RBT_giangvienHR_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_giangvienHR(sender, e);
            TB_maHR.Clear(); TB_hovatenHR.Clear(); TB_sdtHR.Clear(); TB_emailHR.Clear(); TB_diachiHR.Clear(); 
        }

        private void RBT_nhanvienvanphongHR_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_nhanvienHR(sender, e);
            TB_maHR.Clear(); TB_hovatenHR.Clear(); TB_sdtHR.Clear(); TB_emailHR.Clear(); TB_diachiHR.Clear(); 
        }

        private void load_datagrid_tatcaHR(object sender, EventArgs e)
        {
            try
            {
                
                OleDbConnection cnData = new OleDbConnection();
                cnData.ConnectionString = connection_string;
                
                OleDbCommand command = new OleDbCommand();
                command.Connection = cnData;
                command.CommandText = "select * from [teacher]";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHR.DataSource = dt;
                cnData.Close();
                cnData.Dispose();
                cnData = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_giangvienHR(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from [teacher] where [name_position] = 'giảng viên' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHR.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_nhanvienHR(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from [teacher] where [name_position] = 'nhân viên' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHR.DataSource = dt;

                cn.Close();
                cn.Dispose();
                cn = null;

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_timkiemGiangvien(object sender, EventArgs e)
        {
            try
            {

                if (TB_timkiemHR.Text == "")
                {
                    MessageBox.Show("Nhập Tên Giảng Viên", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cn = new OleDbConnection();
                    cn.ConnectionString = connection_string;
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = cn;
                    string fullname = TB_timkiemHR.Text;
                    command.CommandText = "select * from [teacher] where fullname = '" + fullname + "'";
                    OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    DGV_danhsachHR.DataSource = dt;
                    cn.Close();
                    cn.Dispose();
                    cn = null;

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void BT_tuyenHR_Click(object sender, EventArgs e)
        {
            try
            {
                F_NewHR obj = new F_NewHR();
                obj.ShowDialog(this);
                load_datagrid_tatcaHR(sender, e);
               
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_doithongtinHR_Click(object sender, EventArgs e)
        {
            try
            {
                
                if (TB_maHR.Text == "" || TB_hovatenHR.Text == "" || CbB_gioitinhHR.Text == "" || TB_sdtHR.Text == "" || TB_emailHR.Text == "" || TB_diachiHR.Text == "")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cnUpdateHR = new OleDbConnection();
                    cnUpdateHR.ConnectionString = connection_string;
                    cnUpdateHR.Open();
                    OleDbCommand cmdUpdateTTHR = new OleDbCommand();
                    cmdUpdateTTHR.Connection = cnUpdateHR;
                    int id = int.Parse(TB_maHR.Text.ToString());
                    
                    string birthday = DTP_ngaysinhHR.Value.ToShortDateString();
                    string fullname = TB_hovatenHR.Text, gender = CbB_gioitinhHR.Text, phone = TB_sdtHR.Text, email = TB_emailHR.Text, address = TB_diachiHR.Text;
                    string name_position = CbB_chucvuHR.Text;

                    cmdUpdateTTHR.CommandText = " update teacher set fullname = '" + fullname + "' , gender = '" + gender + "' , phone = '" + phone + "' , email = '" + email + "' , address = '" + address + "' , birthday = '" + birthday + "' , name_position = '" + name_position + "'  where id = " + id;

                    cmdUpdateTTHR.ExecuteNonQuery();
                    cnUpdateHR.Close();
                    load_datagrid_tatcaHR(sender, e);
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
      
        private void BT_timkiemHR_Click(object sender, EventArgs e)
        {
            load_datagrid_timkiemGiangvien(sender, e);
        }


//--------------------------------------------------------GB_COURSE-----------------------------------------------------//
        //-----------------------------------------------------------------------------------------------------//
        
        
        private void GB_Course_VisibleChanged(object sender, EventArgs e)
        {
            RBT_tatcaKhoa_CheckedChanged(sender, e);
            DTP_ngaybatdauKhoa.Value = DateTime.Now;
            DTP_ngayketthucKhoa.Value = DateTime.Now;
            DTP_ngaykhoitaoKhoa.Value = DateTime.Now;
        }

        private void DGV_danhsachKhoa_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                int i = DGV_danhsachKhoa.CurrentCell.RowIndex;
                TB_maKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[0].Value.ToString();
                TB_tenKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[1].Value.ToString();
                TB_kyhieuKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[2].Value.ToString();
                TB_hocphiKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[3].Value.ToString();
                DTP_ngaybatdauKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[4].Value.ToString();
                DTP_ngayketthucKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[5].Value.ToString();
                DTP_ngaykhoitaoKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[6].Value.ToString();
                TB_sotiethocKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[7].Value.ToString();
                TB_soHVchoKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[8].Value.ToString();
                int availableCourse = int.Parse(DGV_danhsachKhoa.Rows[i].Cells[9].Value.ToString());
                if (availableCourse == 1) ChB_tinhtrangKhoa.Checked = true;
                else if (availableCourse == 0) ChB_tinhtrangKhoa.Checked = false;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }

        }

        private void RBT_tatcaKhoa_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_tatcakhoahoc(sender, e);
            BT_xoaKhoa.Enabled = false;
            BT_themKhoa.Enabled = true;
            TB_maKhoa.Clear();  TB_kyhieuKhoa.Clear(); TB_tenKhoa.Clear(); TB_sotiethocKhoa.Clear(); TB_hocphiKhoa.Clear(); TB_soHVchoKhoa.Clear();
        }

        private void RBT_khoadangmo_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_khoadanghoc(sender, e);
            BT_xoaKhoa.Enabled = false;
            BT_themKhoa.Enabled = false;
            TB_maKhoa.Clear(); TB_kyhieuKhoa.Clear(); TB_tenKhoa.Clear(); TB_sotiethocKhoa.Clear(); TB_hocphiKhoa.Clear(); TB_soHVchoKhoa.Clear();
        }

        private void RBT_khoadukien_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_khoadangcho(sender, e);
            BT_xoaKhoa.Enabled = true;
            BT_themKhoa.Enabled = true;
            TB_maKhoa.Clear(); TB_kyhieuKhoa.Clear(); TB_tenKhoa.Clear(); TB_sotiethocKhoa.Clear(); TB_hocphiKhoa.Clear(); TB_soHVchoKhoa.Clear();
        }


        private void load_datagrid_tatcakhoahoc(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnkhoa = new OleDbConnection();
                cnkhoa.ConnectionString = connection_string;
                OleDbCommand commandKhoa = new OleDbCommand();
                commandKhoa.Connection = cnkhoa;
                commandKhoa.CommandText = "select * from [course]";
                OleDbDataAdapter adapt = new OleDbDataAdapter(commandKhoa);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachKhoa.DataSource = dt;
                cnkhoa.Close();
                cnkhoa.Dispose();
                cnkhoa = null;

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_khoadanghoc(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnkhoa = new OleDbConnection();
                cnkhoa.ConnectionString = connection_string;
                OleDbCommand commandKhoa = new OleDbCommand();
                commandKhoa.Connection = cnkhoa;
                commandKhoa.CommandText = "select * from [course] where available = 1";
                OleDbDataAdapter adapt = new OleDbDataAdapter(commandKhoa);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachKhoa.DataSource = dt;
                cnkhoa.Close();
                cnkhoa.Dispose();
                cnkhoa = null;

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_khoadangcho(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnkhoa = new OleDbConnection();
                cnkhoa.ConnectionString = connection_string;
                OleDbCommand commandKhoa = new OleDbCommand();
                commandKhoa.Connection = cnkhoa;
                commandKhoa.CommandText = "select * from [course] where available = 0 ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(commandKhoa);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachKhoa.DataSource = dt;
                cnkhoa.Close();
                cnkhoa.Dispose();
                cnkhoa = null;

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


        private void load_datagrid_timkiemKhoa(object sender, EventArgs e)
        {
            try
            {
                if (TB_timkiemKhoa.Text == "")
                {
                    MessageBox.Show("Nhập Mã Khóa Cần Tìm", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cn = new OleDbConnection();
                    cn.ConnectionString = connection_string;
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = cn;
                    string cocode = TB_timkiemKhoa.Text;
                    command.CommandText = "select * from [course] where cocode = '" + cocode+ "'";
                    OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    DGV_danhsachKhoa.DataSource = dt;
                    cn.Close();
                    cn.Dispose();
                    cn = null;

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_timkiemKhoa_Click(object sender, EventArgs e)
        {
            load_datagrid_timkiemKhoa(sender, e);
        }

        private void BT_themKhoa_Click(object sender, EventArgs e)
        {
            try
            {

                F_NewCourse obj = new F_NewCourse();
                obj.ShowDialog(this);
                load_datagrid_tatcakhoahoc(sender, e);

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_themLop_Click(object sender, EventArgs e)
        {
            try
            {

                F_NewClass obj = new F_NewClass();
                obj.getIDCourse(TB_maKhoa.Text);
                obj.getCocode(TB_kyhieuKhoa.Text);
                obj.getConame(TB_tenKhoa.Text);
                obj.getCenter(center);
                obj.ShowDialog(this);

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        
        private void BT_doithongtinKhoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_maKhoa.Text == "" || TB_kyhieuKhoa.Text == "" || TB_tenKhoa.Text == "" || TB_sotiethocKhoa.Text == "" || TB_hocphiKhoa.Text == ""||TB_soHVchoKhoa.Text =="")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cnUpdateKhoa = new OleDbConnection();
                    cnUpdateKhoa.ConnectionString = connection_string;
                    cnUpdateKhoa.Open();
                    OleDbCommand cmdUpdateTTKhoa = new OleDbCommand();
                    cmdUpdateTTKhoa.Connection = cnUpdateKhoa;
                    int id = int.Parse(TB_maKhoa.Text.ToString());
                    int lesson_num = int.Parse(TB_sotiethocKhoa.Text.ToString());
                    int tuition = int.Parse(TB_hocphiKhoa.Text.ToString());
                    int waiting_student = int.Parse(TB_soHVchoKhoa.Text.ToString());

                    string end_date = DTP_ngayketthucKhoa.Value.ToShortDateString();
                    string start_date = DTP_ngaybatdauKhoa.Value.ToShortDateString();
                    string create_date = DTP_ngaykhoitaoKhoa.Value.ToShortDateString();

                    string cocode = TB_kyhieuKhoa.Text, coname = TB_tenKhoa.Text;
                    cmdUpdateTTKhoa.CommandText = " update course set coname = '" + coname + "' , cocode = '" + cocode + "' , tuition = '" + tuition + "' , start_date = '" + start_date + "' , end_date = '" + end_date + "' , create_date = '" + create_date + "' , lesson_num = '" + lesson_num + "' , waiting_student = '" + waiting_student + "'  where id = " + id ;

                    cmdUpdateTTKhoa.ExecuteNonQuery();
                    cnUpdateKhoa.Close();
                    load_datagrid_tatcakhoahoc(sender, e);
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_xoaKhoa_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnDel = new OleDbConnection();
                cnDel.ConnectionString = connection_string;
                cnDel.Open();
                OleDbCommand cmdDelHV = new OleDbCommand();
                cmdDelHV.Connection = cnDel;
                int id = int.Parse(TB_maKhoa.Text.ToString());
                cmdDelHV.CommandText = "delete * from [course] where id = " + id;
                cmdDelHV.ExecuteNonQuery();
                cnDel.Close();
                load_datagrid_khoadangcho(sender, e);

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


    
    }
}
